package com.blog.controller;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blog.bean.Blog;
import com.blog.bean.Record;
import com.blog.bean.User;
import com.blog.dao.IBlogDao;
import com.blog.dao.IRecordDao;
import com.blog.dao.IUserDao;

/**
 * @className: RecordController
 * @description: 
 * @author: HCl
 * @createTime: 2020年6月8日 下午11:19:10
 */

@Controller
public class RecordController {
	
	
	@Autowired
	private IUserDao userDao;
	@Autowired
	private IRecordDao recordDao;
	@Autowired
	private IBlogDao blogDao; 
	
	@RequestMapping(value = "deleteRecord")
	public Map<String, Object> deleteRecord(int id,HttpSession session){
		
		Map<String, Object> reMap = new HashMap<String, Object>();
		User user = (User)session.getAttribute("loginUser");
		Record record = new Record();
		record.setBlog(id);
		record.setFrom(user.getId());
		
		Record getRecord = recordDao.selectRecordByUserIdAndBlogId(record);
		
		int i1 = 0;
		i1 = recordDao.deleteRecord(getRecord.getId());
		System.out.println("删除"+getRecord.getId());
		
		//查询用户最近访问
		List<Record> records = recordDao.selectAllRecordByUserId(user.getId());
		List<Blog> historyBlogs = new LinkedList<Blog>();
		Blog tempBlog = new Blog();
		for (int i = 0; i < records.size(); i++) {
			
			tempBlog = blogDao.selectBlogById(records.get(i).getBlog());
			tempBlog.setCreatedate(records.get(i).getTime());
			historyBlogs.add(tempBlog);
			if(i == 4)break;
			
			
			
			
			
		}
		session.setAttribute("history_blog", historyBlogs);
		
		return reMap;
	}
	
	

}
